package mspbots.data.tl.prod.autotask.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fasterxml.jackson.databind.JsonNode;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import mspbots.data.tl.base.AbstractBaseServiceImpl;
import mspbots.data.tl.entity.ResultEntity;
import mspbots.data.tl.prod.autotask.mapper.TaskAutotaskServiceLevelAgreementResultsMapper;
import mspbots.data.tl.prod.autotask.model.TaskAutotaskServiceCallTicketResources;
import mspbots.data.tl.prod.autotask.model.TaskAutotaskServiceLevelAgreementResults;
import mspbots.data.tl.prod.autotask.service.TaskAutotaskServiceLevelAgreementResultsService;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.time.LocalDateTime;

@Service
@Log4j2
@RequiredArgsConstructor
@DS("master")
public class TaskAutotaskServiceLevelAgreementResultsServiceImpl extends AbstractBaseServiceImpl<TaskAutotaskServiceLevelAgreementResultsMapper, TaskAutotaskServiceLevelAgreementResults>
        implements TaskAutotaskServiceLevelAgreementResultsService {

    private final TaskAutotaskServiceLevelAgreementResultsMapper taskAutotaskServiceLevelAgreementResultsMapper;

    @Override
    public void sync(ResultEntity content){
        log.info("Auto task serviceLevelAgreementResults receiver content{}",content);
        JsonNode entity = content.getEntity();
        TaskAutotaskServiceLevelAgreementResults data=TaskAutotaskServiceLevelAgreementResults.builder()
                .serviceLevelAgreementResultsId(super.getLongValue(entity.get("id")))
                .firstResponseElapsedHours(ObjectUtils.isEmpty(entity.get("firstResponseElapsedHours")) ? null : entity.get("firstResponseElapsedHours").asDouble())
                .firstResponseInitiatingResourceId(ObjectUtils.isEmpty(entity.get("firstResponseInitiatingResourceID")) ? null : entity.get("firstResponseInitiatingResourceID").asInt())
                .firstResponseResourceId(ObjectUtils.isEmpty(entity.get("firstResponseResourceID")) ? null : entity.get("firstResponseResourceID").asInt())
                .isFirstResponseMet(ObjectUtils.isEmpty(entity.get("isFirstResponseMet")) ? null : entity.get("isFirstResponseMet").asBoolean())
                .isResolutionMet(ObjectUtils.isEmpty(entity.get("isResolutionMet")) ? null : entity.get("isResolutionMet").asBoolean())
                .isResolutionPlanMet(ObjectUtils.isEmpty(entity.get("isResolutionPlanMet")) ? null : entity.get("isResolutionPlanMet").asBoolean())
                .resolutionElapsedHours(ObjectUtils.isEmpty(entity.get("resolutionElapsedHours")) ? null : entity.get("resolutionElapsedHours").asDouble())
                .resolutionPlanElapsedHours(ObjectUtils.isEmpty(entity.get("resolutionPlanElapsedHours")) ? null : entity.get("resolutionPlanElapsedHours").asDouble())
                .resolutionPlanResourceId(ObjectUtils.isEmpty(entity.get("resolutionPlanResourceID")) ? null : entity.get("resolutionPlanResourceID").asInt())
                .resolutionResourceId(ObjectUtils.isEmpty(entity.get("resolutionResourceID")) ? null : entity.get("resolutionResourceID").asInt())
                .resourceId(ObjectUtils.isEmpty(entity.get("resourceID")) ? null : entity.get("resourceID").asInt())
                .serviceCallTicketId(ObjectUtils.isEmpty(entity.get("serviceCallTicketID")) ? null : entity.get("serviceCallTicketID").asInt())
                .serviceLevelAgreementName(ObjectUtils.isEmpty(entity.get("serviceLevelAgreementName")) ? null : entity.get("serviceLevelAgreementName").asText())
                .ticketId(ObjectUtils.isEmpty(entity.get("ticketID")) ? null : entity.get("ticketID").asInt())
                .syncTime(LocalDateTime.now())
                .build();
        data.setTenantCode(content.getTenantId());

        TaskAutotaskServiceLevelAgreementResults dbData = super.baseMapper.selectOne(Wrappers.<TaskAutotaskServiceLevelAgreementResults>lambdaQuery()
                .eq(TaskAutotaskServiceLevelAgreementResults::getTenantCode, data.getTenantCode())
                .eq(TaskAutotaskServiceLevelAgreementResults::getServiceLevelAgreementResultsId, data.getServiceLevelAgreementResultsId())
                .last("limit 1"));
        if (ObjectUtils.isEmpty(dbData)){
            super.baseMapper.insert(data);
        } else {
            data.setId(dbData.getId());
            super.baseMapper.updateById(data);
        }


    }
}
